package com.xt.LibaryDeskManager.servlet;

import com.wf.captcha.ArithmeticCaptcha;
import com.xt.LibaryDeskManager.utils.StrUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/Code")
public class CodeController {

    @RequestMapping("/getCode")
    public void getCode(String A,HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(120, 38);
        captcha.setLen(2);
        captcha.getArithmeticString();
        req.getSession().setAttribute("result",captcha.text());
        captcha.out(resp.getOutputStream());
    }

    /**
     * 检验输入的验证码是否正确
     * @param req
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/ExamineCode")
    @ResponseBody
    public Map ExamineCode(String InputResult,HttpServletRequest req) throws ServletException, IOException {
        String result= (String) req.getSession().getAttribute("result");
        int code=0;
        if (InputResult.equals(result)){
            code=1;
        }
        return StrUtils.creatMessage("code",code);
    }
}
